package com.hzy.fivepalace.admin.config.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.hzy.fivepalace.common.consts.CommConsts;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
@ConditionalOnExpression("true")
@ComponentScan("com.hzy.fivepalace.admin.controller")
@EnableKnife4j
public class SwaggerConfig {

    @Value("${server.port}")
    private String serverPort;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("后台管理端")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hzy.fivepalace.admin.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(security());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("嗨美汇-后台管理端接口文档")
                .description("此文档仅供开发人员使用")
                .termsOfServiceUrl("http://127.0.0.1:" + serverPort + "/")
                .contact(new Contact("gejian" , "http://127.0.0.1:" +serverPort + "/" , "ahhzy2017@163.com\n"))
                .version("1.0.0")
                .build();
    }


    private List<SecurityScheme> security() {
        //token
        ApiKey token = new ApiKey("认证token" , CommConsts.ADMIN_LOGIN_TOKEN_KEY, "header");
        List<SecurityScheme> list = new ArrayList<>();
        list.add(token);
        return list;
    }
}
